package cn.doitedu.web_service.cn.doitedu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;

@RestController
public class HelloController {

    @RequestMapping("/api/sort")
    public String sortStr(String str) {
        String[] split = str.split(",");
        int[] datas = Arrays.stream(split).mapToInt(s -> Integer.parseInt(s)).toArray();

        Arrays.sort(datas);

        StringBuilder sb = new StringBuilder();
        for (int data : datas) {
            sb.append(data).append(",");
        }
        sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }

    @RequestMapping("/api/add")
    public String addStr(String a, String b) {
        return (Integer.parseInt(a) + Integer.parseInt(b)) + "";
    }


    @RequestMapping("/api/page")
    public String addStr(String name) {

        String htmlCode = "<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>Title</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "<h1>\n" +
                "welcome " + name + "!\n" +
                "</h1>\n" +
                "</body>\n" +
                "</html>";


        return htmlCode;
    }


    @RequestMapping(value = "/api/xx", method = RequestMethod.POST)
    public PostVo dataOperate(@RequestBody PostVo postVo) {
        double data1 = Double.parseDouble(postVo.getData1());
        double data2 = Double.parseDouble(postVo.getData2());
        String operator = postVo.getOperator();

        Double result = 0.0;
        switch (operator) {
            case "+":
                result = data1+data2;
                break;
            case "*":
                result = data1*data2;
                break;
            case "-":
                result = data1-data2;
                break;
            case "/":
                result = data1/data2;
                break;
            default:
                result = null;
        }

        postVo.setResult(result);
        return postVo;
    }


}
